package com.qf.lock;


import com.qf.util.ZkClient;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.junit.Test;

import java.util.concurrent.TimeUnit;

/**
 * zk分布式锁实现
 * @author zjw
 */
public class ZkLock {

    @Test
    public void lock() throws Exception {
        // 相当于创建了ReentranLock
        InterProcessMutex lock = new InterProcessMutex(ZkClient.getClient(),"/lock");
        try {
            // 获取锁资源
            if (lock.acquire(20000, TimeUnit.MILLISECONDS)) {
                throw new RuntimeException();
            }
        } finally {
            lock.release();
        }
    }
}
